package set_;

import com.sun.org.apache.bcel.internal.generic.NEW;

import java.util.HashSet;
import java.util.Objects;

/**
 * @Author 小闽
 * @Date 2024/11/3 22:57
 * @PackageName: set_
 * @ClassName: HashSetExercise
 * @Description:
 * @Version
 */
@SuppressWarnings({"all"})
public class HashSetExercise {
    public static void main(String[] args) {
        /**
         * 定义一个Employee类，该类包含：private成员属性name,age要求：
         * 1. 创建3个Employee放入 HashSet中
         * 2. 当name和age的值相同时，认为是相同的员工，不能添加到HashSet集合中
         */
        HashSet hashSet = new HashSet();
        Employee jack = new Employee("jack", 12);
        Employee tom = new Employee("tom", 13);
        Employee simth = new Employee("jack", 12); //这里的smith是会被加入到集合中去的，因为他们的hash吗值不同
        hashSet.add(jack);
        hashSet.add(tom);
        hashSet.add(simth); // 当重写了equals方法时候，那么simith就不会加入到hashSet中去了
        System.out.println("hashSet= " + hashSet);
    }
}
class Employee{
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age &&
                Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}